/*!
 * jQuery Query Plugin v1.0.0
 */
(function($) {
    $.query = function(key, value, url) {
        if (!url) {
            url = window.location.href;
        }

        //set
        if (value !== undefined) {
            var keyValue = value === null ? '' : key + "=" + encodeURIComponent(value);
            var reg = new RegExp("([\?&])" + key + "=([^&#]*)", "gi");
            if (url.match(reg)) {
                url = url.replace(reg, "$1" + keyValue);
                if (!keyValue) {
                    url = url.replace(new RegExp("([\?&])&"), "$1");
                    url = url.replace(new RegExp("[\?&](#|$)"), "$1");
                }
            } else if (keyValue) {
                keyValue = (url.indexOf("?") > 0 ? (url.match(new RegExp("[\?](#|$)")) ? "" : "&") : "?") + keyValue;
                if (url.indexOf("#") > 0) {
                    url = url.replace('#', keyValue + "#");
                } else {
                    url += keyValue;
                }
            }
            return url;
        }

        //get
        var result = url.match(new RegExp("[\?&]" + key + "=([^&#]*)", "i"));
        return result ? decodeURIComponent(result[1]) : null;
    }
})(jQuery);